<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form :inline="true" label-width="80px" class="searhForm">
        <div>
          <el-form-item label="模块名称:">
            <el-input
              placeholder="请输入模块名称"
              style="width:184px;height:32px"
              v-model.trim="listQuery.title"
            ></el-input>
          </el-form-item>
          <el-form-item label="是否全局:">
            <el-select
              v-model="listQuery.importance"
              placeholder="是否全局"
              clearable
              style="width: 120px"
              class="filter-item"
            >
              <el-option
                v-for="item in importanceOptions"
                :key="item.type"
                :label="item.val"
                :value="item.type"
              />
            </el-select>
          </el-form-item>
          <el-form-item style="width:136px;">
            <el-button
              type="primary"
              @click="handleFilter()"
              style="width:56px;height:32px;padding:0;margin-left:10px"
              >查询</el-button
            >
          </el-form-item>
        </div>
        <div>
          <el-form-item>
            <el-button type="primary" @click="newadd()">新增</el-button>
            <el-button type="primary" @click="userPoints()">用户积分</el-button>
          </el-form-item>
        </div>
      </el-form>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      size="mini"
      highlight-current-row
      style="width: 100%;"
      @sort-change="sortChange"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" align="center"> </el-table-column>
      <el-table-column
        label="RuleID"
        prop="id"
        sortable="custom"
        align="center"
        width="100"
        :class-name="getSortClass('id')"
      >
        <template slot-scope="{ row }">
          <span>{{ row.KnowledgeId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="规则名称" width="80px" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.Name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="模块名称" width="150px">
        <template slot-scope="{ row }">
          <span class="link-type" @click="handleUpdate(row)">{{
            row.ModuleName
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="积分值" align="center" min-width="150px">
        <template slot-scope="{ row }">
          <span>{{ row.Points }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否全局" width="80px">
        <template slot-scope="{ row }">
          <span>{{ row.IsGlobal ? '是' : '否' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否设置每天积分上限" class-name="status-col">
        <template slot-scope="{ row }">
          <span>{{ row.Issetupper ? '是' : '否' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="开始积分时间" class-name="status-col" width="120">
        <template slot-scope="{ row }">
          <span>{{ row.BeginTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束积分时间" class-name="status-col" width="120">
        <template slot-scope="{ row }">
          <span>{{ row.EndTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否启用" class-name="status-col" width="100">
        <template slot-scope="{ row }">
          <span>{{ row.Enabled ? '是' : '否' }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        >>
        <template slot-scope="{ row }">
          <el-button type="primary" size="mini" @click="newUpdate(row)">
            编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
    <el-dialog
      title="用户积分"
      :visible.sync="dialoguserPoints"
      class="abow_dialog"
    >
      <el-table
        v-loading="listLoading"
        :data="listuser"
        border
        fit
        size="mini"
        highlight-current-row
        style="width: 100%;"
      >
        <el-table-column label="RuleID" prop="id" align="center">
          <template slot-scope="{ row }">
            <span>{{ row.KnowledgeId }}</span>
          </template>
        </el-table-column>
        <el-table-column label="规则名称" align="center">
          <template slot-scope="{ row }">
            <span>{{ row.Name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="模块名称" align="center">
          <template slot-scope="{ row }">
            <span class="link-type" @click="handleUpdate(row)">{{
              row.username
            }}</span>
          </template>
        </el-table-column>
        <el-table-column label="积分值" align="center" min-width="150px">
          <template slot-scope="{ row }">
            <span>{{ row.Points }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
          >>
          <template slot-scope="{ row }">
            <el-button type="primary" size="mini" @click="viewdetails(row)">
              查看明细
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="listuser.length > 0"
        :total="listuser.length"
        :page.sync="listuserQuery.page"
        :limit.sync="listuserQuery.limit"
        @pagination="usergetList"
      />
    </el-dialog>
    <el-dialog title="用户积分" :visible.sync="detailed" class="abow_dialog">
      <el-table
        v-loading="listLoading"
        :data="listuser"
        border
        fit
        size="mini"
        highlight-current-row
        style="width: 100%;"
      >
        <el-table-column label="RuleID" prop="id" align="center">
          <template slot-scope="{ row }">
            <span>{{ row.KnowledgeId }}</span>
          </template>
        </el-table-column>
        <el-table-column label="规则名称" align="center">
          <template slot-scope="{ row }">
            <span>{{ row.Name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="模块名称" align="center">
          <template slot-scope="{ row }">
            <span class="link-type" @click="handleUpdate(row)">{{
              row.username
            }}</span>
          </template>
        </el-table-column>
        <el-table-column label="积分值" align="center" min-width="150px">
          <template slot-scope="{ row }">
            <span>{{ row.Points }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
          >>
          <template slot-scope="{ row }">
            <el-button type="primary" size="mini" @click="viewdetails(row)">
              查看明细
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="listuser.length > 0"
        :total="listuser.length"
        :page.sync="listuserQuery.page"
        :limit.sync="listuserQuery.limit"
        @pagination="usergetList"
      />
    </el-dialog>
  </div>
</template>

<script>
import { fetchList, createArticle, updateArticle } from '@/api/points'
import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination基于el分页的二次封装

export default {
  name: 'pointsManagement',
  components: { Pagination },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      tableKey: 0,
      list: null, // 积分管理列表
      listuser: [
        {
          KnowledgeId: '1',
          Name: '我是规则',
          username: '陈豪',
          Points: '100',
          id: 1
        },
        {
          KnowledgeId: '12',
          Name: '我是规则2',
          username: '陈豪',
          Points: '1001',
          id: 2
        },
        {
          KnowledgeId: '13',
          Name: '我是规则3',
          username: '陈豪',
          Points: '1002',
          id: 3
        },
        {
          KnowledgeId: '14',
          Name: '我是规则4',
          username: '陈豪',
          Points: '1003',
          id: 4
        },
        {
          KnowledgeId: '14',
          Name: '我是规则4',
          username: '陈豪',
          Points: '1003',
          id: 4
        },
        {
          KnowledgeId: '14',
          Name: '我是规则4',
          username: '陈豪',
          Points: '1003',
          id: 4
        },
        {
          KnowledgeId: '14',
          Name: '我是规则4',
          username: '陈豪',
          Points: '1003',
          id: 4
        },
        {
          KnowledgeId: '14',
          Name: '我是规则4',
          username: '陈豪',
          Points: '1003',
          id: 4
        },
        {
          KnowledgeId: '14',
          Name: '我是规则4',
          username: '陈豪',
          Points: '1003',
          id: 4
        },
        {
          KnowledgeId: '14',
          Name: '我是规则4',
          username: '陈豪',
          Points: '1003',
          id: 4
        },
        {
          KnowledgeId: '14',
          Name: '我是规则4',
          username: '陈豪',
          Points: '1003',
          id: 4
        }
      ], // 用户积分列表
      total: 0,
      listLoading: false,
      listQuery: {
        page: 1,
        limit: 20,
        importance: undefined,
        title: undefined,
        type: undefined,
        sort: '+id'
      }, // 分页信息等
      listuserQuery: {
        page: 1,
        limit: 10
      },
      listArray: [{ type: 1 }],
      importanceOptions: [
        { val: '是', type: 1 },
        { val: '否', type: 2 }
      ],
      newaddDialog: false, // 新增或者编辑窗口
      newArray: {
        Id: '',
        Name: '', // 规则名称
        ModuleName: '', // 模块名称
        ModuleId: '', // 模块编号
        Points: '', // 积分值
        IsGlobal: false, // 是否全局
        Issetupper: false, // 是否设置每天积分上限
        BeginTime: '', // 开始积分时间
        EndTime: '', // 结束积分时间
        Enabled: true, // 是否启用
        querycriteria: '', // 业务查询条件
        marker: '' // 描述
      },
      rulesArr: {
        Name: [{ required: true, message: '请设置规则名称', trigger: 'blur' }],
        Id: [{ required: true, message: '请设置RuleID', trigger: 'blur' }],
        Enabled: [
          { required: true, message: '请设置是否启用', trigger: 'blur' }
        ],
        IsGlobal: [
          { required: true, message: '请设置是否全局', trigger: 'blur' }
        ],
        Issetupper: [
          { required: true, message: '是否设置每天积分上限', trigger: 'blur' }
        ],
        ModuleId: [
          { required: true, message: '请设置模块编号', trigger: 'blur' }
        ],
        querycriteria: [
          { required: true, message: '请设置业务查询条件', trigger: 'blur' }
        ]
      },
      newtitle: '新增',
      dialoguserPoints: false, // 用户积分弹窗
      detailed: false // 查看明细
    }
  },
  created() {
    // this.getList()
  },
  methods: {
    //批量操作
    handleSelectionChange(val) {
      console.log('我是批量')
      console.log(val)
    },
    // 规则新增
    newadd() {
      this.$router.push('/example/detail')
      // console.log(this.$router)
      // this.newaddDialog = true;
      // this.newtitle = '新增'
      // this.$nextTick(() => {
      //     this.$refs['newdataForm'].clearValidate()
      // })
    },
    //获取数据
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        console.log(response)
        this.list = response.data.items
        this.total = response.data.total

        // 只是为了模拟请求的时间
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },
    // 获取用户积分信息
    usergetList() {},
    // 查询
    handleFilter() {
      this.listQuery.page = 1
      console.log(this.listQuery)
      this.getList()
    },
    handleModifyStatus(row, status) {
      this.$message({
        message: '操作成功',
        type: 'success'
      })
      row.status = status
    },
    // 排序变化
    sortChange(data) {
      const { prop, order } = data
      if (prop === 'id') {
        this.sortByID(order)
      }
    },
    sortByID(order) {
      if (order === 'ascending') {
        this.listQuery.sort = '+id'
      } else {
        this.listQuery.sort = '-id'
      }
      this.handleFilter()
    },
    newUpdate(row) {
      console.log(row)
      this.$router.push({
        path: '/example/detail',
        query: { points: row.KnowledgeId }
      })
    },
    getSortClass: function(key) {
      const sort = this.listQuery.sort
      return sort === `+${key}` ? 'ascending' : 'descending'
    },
    // 用户积分
    userPoints() {
      this.dialoguserPoints = true
    },
    // 查看明细
    viewdetails(row) {
      console.log(row.id)
      this.detailed = true
    }
  }
}
</script>
<style>
.el-table th.gutter {
  display: table-cell !important;
}
</style>
<style lang="scss" scoped>
.abow_dialog {
  display: flex;
  justify-content: center;
  align-items: Center;
  overflow: hidden;
  ::v-deep .el-dialog {
    margin: 0 auto !important;
    height: 90%;
    overflow: hidden;
    .el-dialog__body {
      position: absolute;
      left: 0;
      top: 54px;
      bottom: 0;
      right: 0;
      // padding: 0;
      z-index: 1;
      // max-height: 84%;
      overflow: hidden;
      overflow-y: auto;
    }
  }
}
</style>
